import json

with open('doc/sql/pcas-code.json', encoding='utf-8') as f:
    data = json.load(f)

def walk(node, level, parent):
    sqls = [f"INSERT INTO sys_region (code, name, level, parentCode) VALUES ('{node['code']}', '{node['name']}', {level}, {f'NULL' if not parent else repr(parent)});\n"]
    for c in node.get('children', []):
        sqls += walk(c, level+1, node['code'])
    return sqls

lines = []
for p in data:
    lines += walk(p, 1, None)

with open('d:/area.sql', 'w', encoding='utf-8') as f:
    f.writelines(lines) 